package com.cmc.cloud.cmclink.doc.service.impl;

import com.cmc.cloud.cmclink.common.report.handler.ReportUrlHandler;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ArrivalNoticeErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.service.FineReportUrlService;
import com.cmc.cloud.cmclink.doc.service.HsCodeService;
import com.cmc.cloud.cmclink.doc.vo.finereporturlvo.FineReportUrlReqVO;
import com.cmc.cloud.cmclink.doc.vo.finereporturlvo.FineReportUrlRespVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 帆软url
 *
 * @author wzb
 * @date 2025/10/15 14:19
 **/
@Service
@Validated
@Slf4j
public class FineReportUrlServiceImpl implements FineReportUrlService {

    @Resource
    private ReportUrlHandler reportUrlHandler;

    @Override
    public FineReportUrlRespVO processUrl(FineReportUrlReqVO inputUrl) {
        if (inputUrl == null || StringUtils.isEmpty(inputUrl.getInputUrl())) {
            log.error("inputUrl is null or empty");
            throw ServiceExceptionUtil.exception(ArrivalNoticeErrorCodeConstants.ARRIVAL_NOTICE_URL_FAILED);
        }
        FineReportUrlRespVO fineReportUrlRespVO = new FineReportUrlRespVO();
        String generateUrl;

        try {
            generateUrl = reportUrlHandler.handle(inputUrl.getInputUrl());
            if (StringUtils.isEmpty(generateUrl)) {
                log.error("generated url is empty");
                throw ServiceExceptionUtil.exception(ArrivalNoticeErrorCodeConstants.ARRIVAL_NOTICE_URL_FAILED);
            }
        } catch (Exception e) {
            log.error("generateUrl failed, inputUrl: {}", inputUrl.getInputUrl(), e);
            throw ServiceExceptionUtil.exception(ArrivalNoticeErrorCodeConstants.ARRIVAL_NOTICE_URL_FAILED);
        }
        fineReportUrlRespVO.setProcessUrl(generateUrl);
        return fineReportUrlRespVO;
    }
}
